//
//  RYYuTopView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/12.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import SDCycleScrollView
import YYText

class RYYuTopView: SYBaseView {
    
    let cycleScrollView: SDCycleScrollView = SDCycleScrollView(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 375.autoWidth() + STATUSBAR_HEIGHT))
    let back: VGImageButton = {
        let v: VGImageButton  = VGImageButton()
        v.imageView.image = UIImage(named: "nav_back")
        return v
    }()
    let fenxiang: UIButton = {
        let v: UIButton  = UIButton()
        v.setImage(UIImage(named: "fenxiang"), for: .normal)
        return v
    }()
    
    let jinduPic: RYJingduBigView = {
        let v: RYJingduBigView = RYJingduBigView()
        v.totalWidth = 286.0.autoWidth()
        return v
    }()
    
    let jiageLb: YYLabel = {
        let v:YYLabel = YYLabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 13.autoFontSize()
        v.textAlignment = .center
        return v
    }()
    
    let titleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.font = 16.autoBoldFontSize()
        
        return v
    }()
    
    let countLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x367FF6)
        v.font = 13.autoFontSize()
        
        return v
    }()
    
    let locationLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x999999)
        v.font = 13.autoFontSize()
        v.text = "西门农场水产基地"
        return v
    }()
    
    let limitLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x999999)
        v.font = 13.autoFontSize()
        
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    let pic: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "dizhi")
        return v
    }()
    
    let lunboPic: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "lunbo_page")
        return v
    }()
    
    let lunboLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor.white
        v.font = 12.autoFontSize()
        v.text = "1 / 5"
        
        return v
    }()
    
    override func initViews() {
        
        cycleView()
        self.addSubview(titleLb)
        self.addSubview(countLb)
        self.addSubview(pic)
        self.addSubview(locationLb)
        self.addSubview(jiageLb)
        self.addSubview(jinduPic)
        self.addSubview(limitLb)
        self.addSubview(sepView)
        
        
        titleLb.snp.makeConstraints { (make) in
            make.top.equalTo(cycleScrollView.snp.bottom).offset(15.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(16.0))
            make.height.equalTo(16.0.autoWidth())
        }
        countLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLb)
            make.right.equalTo(self.snp.right).offset(autoWidth(-16.0))
            make.height.equalTo(16.0.autoWidth())
        }
        pic.snp.makeConstraints { (make) in
            make.top.equalTo(titleLb.snp.bottom).offset(autoWidth(15.0))
            make.left.equalTo(titleLb.snp.left).offset(autoWidth(1.0))
            make.height.equalTo(13.0.autoWidth())
            make.width.equalTo(11.0.autoWidth())

        }
        locationLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(pic)
            make.left.equalTo(pic.snp.right).offset(autoWidth(6.0))
            make.height.equalTo(16.0.autoWidth())
        }
        jiageLb.snp.makeConstraints { (make) in
            make.top.equalTo(locationLb.snp.bottom).offset(autoWidth(15.0))
            make.left.equalTo(titleLb.snp.left).offset(autoWidth(1.0))
            make.height.equalTo(17.0.autoWidth())
        }
        jinduPic.snp.makeConstraints { (make) in
            make.top.equalTo(jiageLb.snp.bottom).offset(autoWidth(14.0))
            make.left.equalTo(titleLb.snp.left)
            make.height.equalTo(25.0.autoWidth())
            make.width.equalTo(286.0.autoWidth())
            make.bottom.equalToSuperview().offset(-20.autoWidth())
        }
        limitLb.snp.makeConstraints { (make) in
            make.top.equalTo(jinduPic)
            make.right.equalTo(self.snp.right).offset(autoWidth(-15.0))
            make.height.equalTo(14.0.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(10.0.autoWidth())
            make.bottom.equalToSuperview()
        }
    }
    
    func configureView(_ model: RYProductDetailModel) {
        
        if let b = model.adoptedPercent {
            let bai = b.replacingOccurrences(of: "%", with: "")
            let dou = bai.doubleValue/100
            jinduPic.currentJindu = dou.saveTwo
        } else {
            jinduPic.currentJindu = 0.01
        }
        titleLb.text = model.name
        countLb.text = "\(model.adoptedNum ?? 0)人认养"
        locationLb.text = model.farmName
        
        let firStr = "￥\(model.adoptPrice ?? "0")"
        let totalStr = "\(firStr)  \(model.adoptHarvest ?? "0")"
        let attString = NSMutableAttributedString(string: totalStr)
        attString.yy_setColor(UIColor(hex: 0xF55035), range: NSRange(location: 0, length: 1))
        attString.yy_setFont(11.autoFontSize(), range: NSRange(location: 0, length:1))
        attString.yy_setColor(UIColor(hex: 0xF55035), range: NSRange(location: 1, length: firStr.count-1))
        attString.yy_setFont(16.autoFontSize(), range: NSRange(location: 1, length:firStr.count-1))
        attString.yy_setColor(UIColor(hex: 0x999999), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        attString.yy_setFont(11.autoFontSize(), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        jiageLb.attributedText = attString
        
        limitLb.text = "限购\(model.purchaseLimit ?? 0)份"
        let urls:[String] = model.carouselImgUrl?.components(separatedBy: ",") ?? []
        var arr:[String] = []
        for (_,value) in urls.enumerated() {
            arr.append("\(IP)\(value)")
        }
        cycleScrollView.imageURLStringsGroup = arr
    }

    func cycleView() {
        
        
//        cycleScrollView.placeholderImage = UIImage(named: "banner")
        cycleScrollView.delegate = self
        back.btn.addAction {
            self.currentVC.navigationController?.popViewController(animated: true)
        }
        fenxiang.addAction {
            
        }
        self.addSubview(cycleScrollView)
        cycleScrollView.addSubview(lunboPic)
        lunboPic.addSubview(lunboLb)
        self.addSubview(fenxiang)
        self.addSubview(back)
        back.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(STATUSBAR_HEIGHT)
            make.height.equalTo(48.autoWidth())
            make.width.equalTo(38.autoWidth())
        }
        
        fenxiang.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.top.equalToSuperview().offset(13.autoWidth() + STATUSBAR_HEIGHT)
            make.height.equalTo(19.autoWidth())
            make.width.equalTo(18.autoWidth())
        }
        lunboPic.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-10.autoWidth())
            make.height.equalTo(21.autoWidth())
            make.width.equalTo(50.autoWidth())
        }
        lunboLb.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(21.autoWidth())
        }
    }
    
}

extension RYYuTopView: SDCycleScrollViewDelegate {
    
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        lunboLb.text = "\(index) / \(cycleScrollView.imageURLStringsGroup.count)"
    }
}
